{include file="my/frameHeader"}
<div>
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格 -->
            <div>
                <table class="layui-hide" id="testTable" lay-filter="test"></table>
                <!-- 表格工具 -->
                <!-- <script type="text/html" id="toolbarDemo">
                 <div class="layui-btn-container">
                     <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                     <button class="layui-btn layui-btn-sm" lay-event="batchdel">批量删除</button>
                 </div>
             </script> -->
            </div>
            <!-- submit -->
            <div class="right-btn">
                <button class="layui-btn J-pass">同意</button>
                <button class="layui-btn J-no-pass">不同意</button>
            </div>
            <form class="layui-form layui-fluid J-add-form" lay-filter="formTest">
                <input type="hidden" class="form-control" id="taskid" name="taskid" value="" placeholder="请输入内容" />
                <input type="hidden" class="form-control" id="schoolid" name="schoolid" value="985" placeholder="请输入内容" />
                <input type="hidden" class="form-control" id="uid" name="uid" value="1" placeholder="请输入内容" />
            </form>
        </div>
    </div>
    <!-- <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">添加监考老师</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">查看监考申请</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">最终监考名单</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script> -->
    <script>
        var table;
        var requireUrl = {
            add: "{:url('test/teacheradd')}",
            edit: "{:url('test/teacheredit')}",
            check: "{:url('test/testappcheck')}",
            list: "{:url('test/getteachertestapp')}" + '?taskid=' + getUrlVariable('taskid'),
        };

        $(function () {
            initClass('teacherlist');
            initTable();
            layuiDate('.J-date');
            $('#taskid').val(getUrlVariable('taskid'));

            // 删除
            $('.J-remove').click(function () {
                var data = {
                    id: $(this).attr('data-id'),
                };
                layerConfirm("{:url('test/teachermove')}", data);
            });

            // 审核
            $('.J-pass').click(function () {
                checkApply(1);
                return false;
            });
            $('.J-no-pass').click(function () {
                checkApply(2);
                return false;
            });
        });

        function initTable() {
            // 请求路径

            layui.use('table', function () {
                table = layui.table;
                table.render({
                    elem: '#testTable',
                    url: requireUrl.list,
                    toolbar: '#toolbarDemo',
                    title: '老师列表',
                    totalRow: false,
                    height: 'full-100',
                    cols: [
                        [
                            { type: 'checkbox', fixed: 'left' },
                            { field: 'id', title: '序号' },
                            { field: 'teachername', title: '老师姓名' },
                            { field: 'demo', title: '申请理由' },
                            { field: 'create_time', title: '创建时间' },
                            // , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 500 }
                        ],
                    ],
                    id: 'testReload',
                    page: true,
                    parseData: function (res) {
                        return {
                            code: 0,
                            msg: '',
                            count: res.total,
                            data: res.record,
                        };
                    },
                });

                //工具栏事件
                table.on('toolbar(test)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            tableAdd({
                                addUrl: requireUrl.add,
                                layerTemp: $('.J-temple'),
                                title: '添加参考老师',
                            });
                            break;
                        case 'batchdel':
                            tableBatchDel(checkStatus, requireUrl.remove);
                            break;
                    }
                });

                //监听行工具事件
                table.on('tool(test)', function (obj) {
                    var data = obj.data;
                    console.log(data.id);
                    if (obj.event === 'del') {
                        layerConfirm(requireUrl.remove, {
                            id: data.id,
                        });
                    } else if (obj.event === 'edit') {
                        layerOpenInit(
                            '参考老师编辑',
                            $('.J-temple'),
                            function (index) {
                                var data = $('form').serialize();
                                ajaxRequest('post', requireUrl.edit, data, function (data) {
                                    data.code == 1 ? successTrip(data) : errorAlert(false, data.msg);
                                });
                            },
                            function () {
                                ajaxRequest(
                                    'get',
                                    requireUrl.edit,
                                    {
                                        id: data.id,
                                    },
                                    function (res) {
                                        form.val('formTest', {
                                            //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                                            id: res.data.id, // "name": "value"
                                            kechengid: res.data.kechengid,
                                            taskid: res.data.taskid,
                                            demo: res.data.demo,
                                        });
                                    }
                                );
                            }
                        );
                    }
                });
            });

            // 搜索重置操作
            var active = {
                reload: function () {
                    var queryParams = serializeObject($('.J-search-form'));
                    console.log(queryParams);
                    //执行重载
                    table.reload(
                        'testReload',
                        {
                            page: {
                                curr: 1, //重新从第 1 页开始
                            },
                            where: queryParams,
                        },
                        'data'
                    );
                },
            };
            $('.J-search').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
            $('.J-reload').on('click', function () {
                $('.J-query-params').val('');
                form.render('select');
                active.reload ? active.reload.call(this) : '';
            });
        }

        function checkApply(check) {
            var checkStatus = table.checkStatus('testReload');
            var userInfo = {
                check: check,
                taskid: getUrlVariable('taskid'),
            };

            var data = checkStatus.data;
            if (data.length == 0) {
                layer.msg('请至少选择一条记录！', { icon: 7 });
                return;
            }

            var delList = [];
            for (var i = 0; i < data.length; i++) {
                delList.push(data[i].id);
            }

            var ids = JSON.stringify(delList);
            userInfo.ids = ids;
            ajaxRequest('post', requireUrl.check, userInfo, function (data) {
                var errMesage = data.msg ? data.msg : data;
                data.code == 1 ? successTrip(data) : errorAlert(false, errMesage);
                parent.location.reload();
            });
        }
    </script>

    {include file="my/footer"}
</div>
